#ifndef __JSONCPP_SHARE_H__
#define __JSONCPP_SHARE_H__
// #include <string>
// #include "json/json.h"
#include "serialize/JsonDeserializer.h"
#include "serialize/JsonSerializer.h"

namespace Json
{

    extern std::string json2string(Json::Value j, bool simple = true);
    extern bool string2json(Json::Value *j, const char *str);
    extern bool jsonfromfile(Json::Value &j, std::string file);
    extern bool json2file(Json::Value &j, std::string file);

    // 单个class -> json 字符串
    template <typename T>
    std::string class2jsonstring(T &t)
    {
        try
        {
            JsonSerializer js;
            std::string serialString = js.Serialize(t);
            return serialString;
        }
        catch (std::string m)
        {
            std::cout << "Error,class2jsonstring:" << m << std::endl;
            return "";
        }
        catch (...)
        {
            return "";
        }
    }

    // 多个class->json 字符串，  class 以指针方式保存
    template <typename T>
    std::string class2jsonstring(std::vector<T *> plist)
    {
        int i = 0;
        int size = plist.size();
        try
        {
            std::stringstream w;
            w << "[";
            for (i = 0; i < size; i++)
            {
                JsonSerializer js;
                std::string serialString = js.Serialize(*plist[i]);
                w << serialString;
                if (i < (size - 1))
                    w << ",";
            }
            w << "]";
            return w.str();
        }
        catch (std::string msg)
        {
            std::cout << "Error, class2jsonstring,plist[" << i << "],msg = " << msg << std::endl;
            return "";
        }
        catch (...)
        {
            return "";
        }
    }

    // 多个class->json 字符串，  class 以object 方式保存
    template <typename T>
    std::string class2jsonstring(std::vector<T> plist)
    {
        int i = 0;
        int size = plist.size();
        try
        {
            std::stringstream w;
            w << "[";
            for (i = 0; i < size; i++)
            {
                JsonSerializer js;
                std::string serialString = js.Serialize(plist[i]);
                w << serialString;
                if (i < (size - 1))
                    w << ",";
            }
            w << "]";
            return w.str();
        }
        catch (std::string msg)
        {
            std::cout << "Error, class2jsonstring,plist[" << i << "],msg = " << msg << std::endl;
            return "";
        }
        catch (...)
        {
            return "";
        }
    }

    template <typename T>
    bool jsonstring2class(std::string str, T &t)
    {
        JsonDeserializer jdl(str);
        try
        {
            jdl.Deserialize(t);
            return true;
        }
        catch (std::string msg)
        {
            std::cout << " jsonstring2class: " << msg << std::endl;
            return false;
        }
        catch (...)
        {
            return false;
        }
        return false;
    }

}

#endif // ! __JSONCPP_SHARE_H__
